package com.itheima.controller;

import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.PageResult;
import com.itheima.common.Result;
import com.itheima.domain.OrderVO;
import com.itheima.domain.Orders;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 86170
 * @createTime 2022/6/5 15:06
 * @description 订单模块-控制器
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    /*
     * @author sch
     * @createTime 2022/6/5 *
     * @desc 提交订单
     * @param
     * @return
     */
    @PostMapping("/submit")
    public Result submitOrder(@RequestBody Orders orders){
        int row = 0;
        //1.调用Service,返回业务处理结果
        row = orderService.submitOrder(orders);
        //2.封装返回
        if(row<1){
            return new Result(Code.ERROR, MessageConstant.ADD_ORDER_ERROR);
        }
        return new Result(Code.OK,MessageConstant.ADD_ORDER_SUCCESS);
    }
    /*
     * @author sch
     * @createTime 2022/6/5 15:38
     * @desc 查询订单列表
     * @param page
     * @return
     */
    @GetMapping("/userPage")
    public PageResult page (@RequestParam("page") Integer page,@RequestParam("pageSize")Integer pageSize){
        List<OrderVO>vos=orderService.page2(page,pageSize);
        if(vos==null){
            vos=new ArrayList<>();
        }
        return new PageResult(Code.OK,MessageConstant.GET_ORDER_SUCCESS,vos);
    }
}
